package com.outsider.util.weather;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import com.outsider.util.OkHttp3;

public class WeatherApi {
    private static OkHttp3 httpUtil=OkHttp3.getInstance();
    private static Map<String,String> header;
    static{
        header=new HashMap<>();
    	header.put("Authorization", PropKit.get("appCode"));
    }
    
    private static String url="http://ali-weather.showapi.com/gps-to-weather?from=5&need3HourForcast=1&needIndex=1&";
    public static SimpleWeather getTodayWeather(String longitude, String latitude){
    	String json=httpUtil.getWithHeader(url+"lat="+latitude+"&lng="+longitude, header);
    	JSONObject temp=JSONObject.parseObject(json);
    	if (0==temp.getInteger("showapi_res_code")){
    		JSONObject body=temp.getJSONObject("showapi_res_body");
        	SimpleWeather simpleWeather=body.getObject("f1", SimpleWeather.class);
        	
        	//生活指数数据
        	JSONObject index=body.getJSONObject("f1").getJSONObject("index");
        	simpleWeather.setTravelDesc(index.getJSONObject("travel").getString("desc"));
        	simpleWeather.setLsDesc(index.getJSONObject("ls").getString("desc"));
        	simpleWeather.setXqDesc(index.getJSONObject("xq").getString("desc"));
        	
        	//预测数据
        	JSONObject myForcast=simpleWeather.getHourForcast();
        	List<String> hourList=new ArrayList<>();
        	List<Integer> maxTem=new ArrayList<>();
        	List<Integer> minTem=new ArrayList<>();
        	myForcast.put("hour", hourList);
        	myForcast.put("max", maxTem);
        	myForcast.put("min", minTem);
        	JSONArray forcastData=body.getJSONObject("f1").getJSONArray("3hourForcast");
        	for (int i=0; i<forcastData.size();i++){
        		JSONObject t=forcastData.getJSONObject(i);
        		hourList.add(t.getString("hour"));
        		maxTem.add(Integer.parseInt(t.getString("temperature_max")));
        		minTem.add(Integer.parseInt(t.getString("temperature_min")));
        	}
        	//城市数据
        	JSONObject cityInfo=body.getJSONObject("cityInfo");
        	String loc=cityInfo.getString("c9")+" "+cityInfo.getString("c7")+" "+cityInfo.getString("c5");
        	simpleWeather.setLoc(loc);
        	
        	//未来两日数据
        	JSONObject f2=body.getJSONObject("f2");
        	JSONObject f3=body.getJSONObject("f3");
        	
        	JSONObject myF2=new JSONObject();
        	JSONObject myF3=new JSONObject();
        	JSONObject futureData=new JSONObject();
        	futureData.put("f2", myF2);
        	futureData.put("f3", myF3);
        	simpleWeather.setFutureData(futureData);
        	
        	myF2.put("date", "明天");
        	myF3.put("date", "后天");
        	myF2.put("jiangshui", f2.getString("jiangshui"));
        	myF3.put("jiangshui", f3.getString("jiangshui"));
        	myF2.put("dayAirtemperature", f2.getString("day_air_temperature"));
        	myF3.put("dayAirtemperature", f3.getString("day_air_temperature"));
        	myF2.put("nightAirTemperature", f2.getString("night_air_temperature"));
        	myF3.put("nightAirTemperature", f3.getString("night_air_temperature"));
        	myF2.put("dayWeather", f2.getString("day_weather"));
        	myF3.put("dayWeather", f3.getString("day_weather"));
            //其他数据
            JSONObject now=body.getJSONObject("now");
            simpleWeather.setSd(now.getString("sd"));
            simpleWeather.setNowTemperature(now.getString("temperature"));
            simpleWeather.setWindDirection(now.getString("wind_direction"));
            simpleWeather.setWindPower(now.getString("wind_power"));
            simpleWeather.setAirQuality(now.getJSONObject("aqiDetail").getString("quality"));
        	return simpleWeather;
    	}
    	else{
    		Logger.getLogger("").info("getTodayWeather error code "+temp.getInteger("showapi_res_code"));
    		return null;
    	}
    }
    
}
